package com.kh.pajx.sop.view.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.SysDictionary;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.kh.pajx.sop.util.common.ValidatorUtils;
import com.kh.pajx.sop.util.page.QueryHelper;
import com.kh.pajx.sop.util.sop.PropertyValidate;
import com.opensymphony.xwork2.ActionContext;

/**
 * 字典信息 控制器
 * 
 * @author: azzcsimp
 * @Createdate: 2014年6月26日 下午3:37:20
 */
@Controller
@Scope("prototype")
public class SysDictionaryAction extends ModelDrivenBaseAction<SysDictionary> {

	private static final long serialVersionUID = 1920291280287143689L;

	private String[] dicIds;

	public String[] getDicIds() {
		return dicIds;
	}

	public void setDicIds(String[] dicIds) {
		this.dicIds = dicIds;
	}

	/** 列表 */
	public String list() throws Exception {
		// 准备页面 字典类型信息
		List<String> dicTypeNameList = sysDictionaryService.getDicTypeNameList();
		ActionContext.getContext().put("dicTypeNameList", dicTypeNameList);

		// 准备分页信息
		new QueryHelper(SysDictionary.class, "d")
				.addCondition(StringUtils.isNotEmpty(model.getDicTypeName()), "d.dicTypeName=?", model.getDicTypeName())
				.addOrderProperty(StringUtils.isNotEmpty(orderField)&&exists(orderField), "d." + orderField, getOrderDirection())
				.addOrderProperty("d.dicTypeCode", true).preparePageBean(sysDictionaryService, pageNum, numPerPage);
		return "list";
	}

	/** 删除 */
	public String delete() throws Exception {
		sysDictionaryService.delete(dicIds);
		return "toList";
	}

	/** 添加页面 */
	public String addUI() throws Exception {
		return "saveUI";
	}

	/** 添加 */
	public void add() {
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			//参数校验
			 if (!ValidatorUtils.validateSafeString(model.getDicName(), 60, false)) {
					throw new Exception("字典名称输入不合法");
				}
			 if (!ValidatorUtils.validateLetterAndNum(model.getDicCode(), 60,false)) {
					throw new Exception("字典编码输入不合法");
				}
			 if (!ValidatorUtils.validateSafeString(model.getDicTypeName(), 60, false)) {
					throw new Exception("字典类型名称输入不合法");
				}
			 if (!ValidatorUtils.validateNumber(model.getDicTypeCode(), 4,false)) {
					throw new Exception("字典类型编码输入不合法");
				}
			
			 
			// ==> 1.校验dicTypeCode是否已存在, 如果已存在字典类型编码，则要验证字典名称输入是否正确
			if (sysDictionaryService.checkRepeatDicTypeCode(StringUtils.trim(model.getDicTypeCode()))) {
				
				// ==>2.校验dicTypeName输入是否符合
				if (sysDictionaryService.checkRepeatDicTypeName(StringUtils.trim(model.getDicTypeCode()),
						StringUtils.trim(model.getDicTypeName()))) {
					// ==>3.校验同一dicTypeCode下，dictCode是否唯一
					if (sysDictionaryService.checkRepeatDicCode(StringUtils.trim(model.getDicCode()),
							StringUtils.trim(model.getDicTypeCode()))) {
						throw new Exception("字典编码：" + model.getDicCode() + "已存在！");
					} else {
						// ==> 4.执行保存操作
						sysDictionaryService.save(model);
						dwz.setMessage("保存成功");
						dwz.setNavTabId("dic_list");
						dwz.setStatusCode("200");
						dwz.setCallbackType("forward");
						dwz.setForwardUrl("dict_list.action");
						dwz.setCloseDialog("dict_addUI");
						DwzAjaxResult.josnMessage(dwz);
					}
				} else {
					throw new Exception("已存在字典类型编码：" + model.getDicTypeCode() + "," + model.getDicTypeName()
							+ "输入与已存在字典类型名称不一致！");
				}
			} else {
				// 如果dicTypeCode 进行保存操作，
				if (sysDictionaryService.checkRepeatDicCode(StringUtils.trim(model.getDicCode()),
						StringUtils.trim(model.getDicTypeCode()))) {
					throw new Exception("字典编码：" + model.getDicCode() + "已存在！");
				} else {
					// ==> 4.执行保存操作
					sysDictionaryService.save(model);
					dwz.setMessage("保存成功");
					dwz.setNavTabId("dic_list");
					dwz.setStatusCode("200");
					dwz.setCallbackType("forward");
					dwz.setForwardUrl("dict_list.action");
					dwz.setCloseDialog("dict_addUI");
					DwzAjaxResult.josnMessage(dwz);
				}
			}
		} catch (Exception ex) {
			dwz.setMessage("保存失败!错误信息：" + ex.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}

	}

	/** 修改页面 */
	public String editUI() throws Exception {
		return "editUI";
	}

	/** 修改 */
	public String edit() throws Exception {
		return "toList";
	}

	/** 字典类型名称提示补全 */
	public String suggest() {
		List<?> dicTypeList = sysDictionaryService.getDicTypeListByTypeName(model.getDicTypeName());
		ActionContext.getContext().put("dicTypeList", dicTypeList);
		return "suggest";
	}
}
